/**
* 
* ====================================================================
*
* @author apov <apov@ymail.com>
*/ 
#pragma once

#include <BaseDefine.h>
#include "Thread.h"
#include "Work.h"
#include <DoubleQueue.h>
#include <PoplarPool.h>

class DBQueryQ : public CThread{
public:	
	enum{
		DEF_MAX_QUE_SIZE=			100,
		DEF_BUFFER_LEN=				1024,
		DEF_EMPTY_Q_SLEEP_TICK=		30,
	};

	size_t add_query( const char* buffer, size_t size );
	WorkListener* getListener(){ return worker_; }

	bool initialize( WorkListener* worker, Work* work_obj, \
		bool query_fail_reload = true, char* name = "no_name", \
		size_t warn_max_que = DEF_MAX_QUE_SIZE );

	virtual DEF_THREAD_RETURN run();

	DBQueryQ();
	virtual ~DBQueryQ();

protected:

private:
	typedef struct QueryObj{
		char buffer_[ DEF_BUFFER_LEN ];
	};
	
	typedef DoubleQueue< QueryObj* > Que;		

	QueryObj* getBuffer();
	void freeBuffer( QueryObj* buffer );
	
	ObjectPool< QueryObj > instances_;

	Que q_;	

	CLock lock_;
	CLock buffer_lock_;

	WorkListener* worker_;
	Work* work_obj_;
	bool query_fail_reload_;
	std::string name_;
	size_t warn_que_size_;
};	
